package com.modbus.tool.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.modbus.tool.entity.po.CollectedData;
import com.modbus.tool.entity.po.DataPoint;
import com.modbus.tool.entity.po.Device;
import com.modbus.tool.mapper.CollectedDataMapper;
import com.modbus.tool.mapper.DataPointMapper;
import com.modbus.tool.mapper.DeviceMapper;
import com.modbus.tool.service.IDeviceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;

import java.util.List;

import com.modbus.tool.common.Result;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: Leno
 * @Date: 2025-08-07
 * @Description: DeviceServiceImpl
 */
@Service
@RequiredArgsConstructor
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements IDeviceService {

    private final DeviceMapper deviceMapper;

    private final CollectedDataMapper collectedDataMapper;

    private final DataPointMapper dataPointMapper;

    @Override
    public Result<Page<Device>> findPage(Integer pageNum, Integer pageSize, Device device) {
        Page<Device> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Device> lqw = new LambdaQueryWrapper<>();
        // 添加条件...
        if (StrUtil.isNotBlank(device.getName())) {
            lqw.like(Device::getName, device.getName());
        }
        if (device.getProtocolType() != null) {
            lqw.eq(Device::getProtocolType, device.getProtocolType());
        }
        return Result.success(page(page, lqw));
    }

    @Override
    @Transactional
    public Result<String> deleteBatch(List<Long> ids) {
        if (ids.isEmpty()) {
            return Result.error("请选择要删除的记录");
        }
        // 同时删除点位和数据
        dataPointMapper.delete(new LambdaQueryWrapper<DataPoint>().in(DataPoint::getDeviceId, ids));
        collectedDataMapper.delete(new LambdaQueryWrapper<CollectedData>().in(CollectedData::getDeviceId, ids));
        removeByIds(ids);
        return Result.successMsg("删除成功");
    }

    @Override
    public Boolean addOrUpdate(Device device) {

        // 参数校验
        // 协议类型: 0=TCP, 1=RTU, 2=RTU over TCP
        Byte protocolType = device.getProtocolType();
        if (protocolType == 0 || protocolType == 2) {
            if (device.getIpAddress() == null) {
                throw new IllegalArgumentException("IP地址不能为空");
            }
            if (device.getPort() == null) {
                throw new IllegalArgumentException("端口号不能为空");
            }
            if (device.getSlaveAddress() == null) {
                throw new IllegalArgumentException("从站地址不能为空");
            }
            if (device.getStartAddress() == null) {
                throw new IllegalArgumentException("寄存器起始地址不能为空");
            }
            if (device.getRegisterCount() == null) {
                throw new IllegalArgumentException("读取数量不能为空");
            }
        }

        if (protocolType == 1) {
            if (device.getSerialPort() == null) {
                throw new IllegalArgumentException("串口设备名称不能为空");
            }
            if (device.getBaudRate() == null) {
                throw new IllegalArgumentException("波特率不能为空");
            }
            if (device.getDataBits() == null) {
                throw new IllegalArgumentException("数据位不能为空");
            }
            if (device.getParity() == null) {
                throw new IllegalArgumentException("校验位不能为空");
            }
            if (device.getStopBits() == null) {
                throw new IllegalArgumentException("停止位不能为空");
            }
            if (device.getSlaveAddress() == null) {
                throw new IllegalArgumentException("从站地址不能为空");
            }
            if (device.getStartAddress() == null) {
                throw new IllegalArgumentException("寄存器起始地址不能为空");
            }
            if (device.getRegisterCount() == null) {
                throw new IllegalArgumentException("读取寄存器数量不能为空");
            }
        }
        return saveOrUpdate(device);
    }

}

